var Enemy = Tank.extend({
    _gold : 0,
    _preDir : 0,
    _fireSpace : 2,
    ///////////////////////
    ctor : function(filename,hp,speed,gold){
        this._super(filename,hp,speed);
        this._gold = gold;

        this.moveState(true);
    },
    onEnter:function () {
        this._super();
        var dir=this.getDirectRand();
        this.swerve(dir);

        this.schedule( this.firing,this._fireSpace );
    },
    onExit : function(){
        this._super();
    },
    ///////////////////////////////
    getDirectRand : function(){
        var dir = GetRandom(0,3);
        if(dir=== this._preDir){
            if(--dir<0)dir+=4;
        }
        this._preDir = dir;
        return  dir;
    },
    /////////////////////////////////////////////////////
    dead : function(i){
        PlayEnemyEffect(0,this.getPosition());
        AddUIMoney(this._gold);
        this._life = false;
        this.setVisible(false);
    },
    //////////////////////////////////////////////////
    toUp : function(){
        // alter skins
        var filename = MainResource( "ene00.png" );
        var frame = new cc.SpriteFrame( filename,cc.rect(0,0,80,80) );
        this.setSpriteFrame(frame);
    },
    toRight : function(){
        var filename = MainResource( "ene02.png" );
        var frame = new cc.SpriteFrame( filename,cc.rect(0,0,80,80) );
        this.setSpriteFrame(frame);
        this.setScaleX(1);
    },
    toDown : function(){
        var filename = MainResource( "ene01.png" );
        var frame = new cc.SpriteFrame( filename,cc.rect(0,0,80,80) );
        this.setSpriteFrame(frame);
    },
    toLeft : function(){
        this.toRight();
        this.setScaleX(-1);
    },
    //////////////////////////////
    delayToSwerve : function(dt){
        this.swerve(this.getDirectRand());
    },
    ////////////////////////////////////////////////////
    newBullet : function(){
        cc.log("new a hero bullet");
        var name = MainResource( "eB00.png" );
        var bullet = new Bullet( name,5,500);
        bullet.setType(1);
        // add bullet to scene
        AddEBullet(bullet);
        return bullet;
    },
    ////////////////////// firing //////////////////////
    firing : function(dt){
        if(this._life) {
            var bullet = GetEBulletFromBuffer();
            if (null == bullet) {
                bullet = this.newBullet();
            }
            bullet.revive(this);
        }
    },
    //////////////////////moving////////////////////////////
    moving : function( dt  ){
        if( this._canMove ){
            var np = this.getPosition();
            np.x += this._xSpeed*this._speed*dt;
            np.y += this._ySpeed*this._speed*dt;
            if( !Blocked(np.x,np.y,this._direct) ){
                this.setPosition(np.x,np.y);
            }else{
                cc.log( "blocked direction" );
                this.scheduleOnce( this.delayToSwerve,GetRandom(0,1) );
            }
        }
    }
});

